package com.orangelife.service.drool;

import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class BillDrools {
	
	
	public static void main(String[] args) {
		
		String rule= "package com.orangelife.service.bill\n"
	+"import com.orangelife.service.drool.BillParms;\n"
	+"import com.orangelife.utils.DecimalUtil;\n"
	+"import java.util.Map;\n"
	+"import java.math.BigDecimal;\n"
	+"rule \"dbbbb\"\n"
	 +"no-loop false\n"
	+"salience 1\n"
	+"when\n"
	  +"      $m : BillParms(type==\"2\")\n"
	    +"then\n"

	    
	      +"  $m.setRes( DecimalUtil.add(\"8782\"));\n"
	   
	+"end";
		
		KieSession kk=BillDrools.getKieSessionbyCommID2("22",rule);
		BillParms b=new BillParms();
		b.setType("2");
		
		kk.insert(b);
		kk.fireAllRules();
		System.out.println(b.getRes().toString());
		
		String rule2= "package com.orangelife.service.bill\n"
	+"import com.orangelife.service.drool.BillParms;"
	+"import com.orangelife.utils.DecimalUtil;"
	+"import java.util.Map;"
	+"import java.math.BigDecimal;"
	+"rule \"dbbbb\""
	 +"no-loop false\n"
	+"salience 1 \n"
	+"when\n"
	  +"      $m : BillParms(type==\"2\")\n"
	    +"then\n"

	    
	      +"  $m.setRes( DecimalUtil.add(\"878882\"));\n"
	   
	+"end";
		KieSession kk1=BillDrools.getKieSessionbyCommID2("22",rule2);

		b.setType("2");
		
		kk1.insert(b);
		kk1.fireAllRules();
		System.out.println(b.getRes().toString());
		BillParms b1=new BillParms();
		b1.setType("2");
		
		kk.insert(b1);
		kk.fireAllRules();
		System.out.println(b1.getRes().toString());
		
	
	}
	
	

	public static  KieSession getKieSessionbyCommID(String commID){
		
		
		KieServices ks = KieServices.Factory.get();
		KieRepository kr =ks.getRepository();
		KieFileSystem kfs=ks.newKieFileSystem();

		kfs.write("src/main/resources/rules1/"+commID+"5.drl",ks.getResources().newClassPathResource("rules1/"+commID+".drl"));
	
		KieBuilder kb=ks.newKieBuilder(kfs);
		
		kb.buildAll();
	
		System.out.println(kb.getResults().getMessages()+"***************");
	    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
     	KieSession kSession = kContainer.newKieSession();
//     	 KieContainer kContainer=ks.getKieClasspathContainer();
//  	    KieSession kSession = kContainer.newKieSession("ksession-rules1");
		System.out.println(ks.newEnvironment());
		return kSession;
	} 
	
	public static  KieSession getKieSessionbyCommID2(String commID,String mmm){
		
		
		KieServices ks = KieServices.Factory.get();
		KieRepository kr =ks.getRepository();
		KieFileSystem kfs=ks.newKieFileSystem();

	//	kfs.write("src/main/resources/rules1/"+commID+"5.drl",ks.getResources().newClassPathResource("rules1/"+commID+".drl"));
		kfs.write("src/main/resources/rules1/"+commID+"5.drl", mmm);
		KieBuilder kb=ks.newKieBuilder(kfs);
		
		kb.buildAll();
	
		System.out.println(kb.getResults().getMessages()+"***************");
	    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
     	KieSession kSession = kContainer.newKieSession();
//     	 KieContainer kContainer=ks.getKieClasspathContainer();
//  	    KieSession kSession = kContainer.newKieSession("ksession-rules1");
		System.out.println(ks.newEnvironment());
		return kSession;
	}


}
